<?php
//图片上传
function uploadImage($image,$addwater = 0){
	$upload = new \Think\Upload();// 实例化上传类    
	$upload->maxSize   =     (int)C('IMG_EXTS')*1024*1024;// 设置附件上传大小
	$upload->exts      =     C('IMG_EXTS');// 设置附件上传类型
	$upload->rootPath  =      C('IMG_ROOTPATH'); // 设置附件上传目录
	// 上传文件     
	$info   =   $upload->uploadOne($_FILES[$image]);    
	if(!$info) {// 上传错误提示错误信息
		return $upload->getError();
	}
	else{
		// 上传成功
        $img = $info['savepath'].$info['savename'];
        if($addwater != 1){
             return $img;
        }
        $setimg = M('config')->field(array('water','w_image','w_opacity','w_text'))->where(array('id'=>1))->find();
        if($setimg['water'] == 1){
            $image = new \Think\Image();
            $image->open($upload->rootPath.$img)->water($upload->rootPath.$setimg['w_image'],\Think\Image::IMAGE_WATER_SOUTHEAST,$setimg['w_opacity'])->save($upload->rootPath.$img);
        }
        else if($setimg['water'] == 2){
            $image->open('./Public/uploads/'.$img)->text($setimg['w_text'],'./1.ttf',20,\Think\Image::IMAGE_WATER_SOUTHEAST)->save('./Public/uploads/'.$img);
        }
        return $img;
	}
}
//删除图片
function delImage($images){
    $rp = C('IMG_rootPath');
    if(!is_array($images)){
    	@unlink($rp . $images);
    }
    foreach($images as $v){
        // @错误抵制符：忽略掉错误,一般在删除文件时都添加上这个
        @unlink($rp . $v);
    }
}

//初始化I函数
function removeXSS($val){
    static $obj = null;
    //实现一个単例模式，这个函数被调用多次时都使用第一次实例化的对象
    if($obj === null){
        require 'Public/HTMLPurifier/HTMLPurifier.includes.php';
        $config = HTMLPurifier_Config::createDefault();
        //保留a标签的target属性
        $config->set('HTML.TargetBlank', TRUE);
        $obj = new HTMLPurifier($config);
    }
    return $obj->purify($val);
}

//获取角色名称
function getGroupName($uid){
    $sql = "select `title` from `qiye_auth_group` as a inner join `qiye_auth_group_access` as b on a.id = b.group_id where `uid` =".$uid;
    $result = M()->query($sql);
    return implode(',', $result[0]);
}